//
//  CollectionFirstView.m
//  MyProject
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//

#import "CollectionFirstView.h"
#import "CollectionViewCell.h"
#import "WHZAfnetWorking.h"
#import "UIImageView+WebCache.h"

#import "ShopJumpViewController.h"

@implementation CollectionFirstView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self create];
    }
    return self;
}

- (void)create{
    
    self.flayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:self.flayout];
    [self addSubview:self.collectionView];
    //     self.frame.size.width, self.frame.size.height / 66 * 35
    //    CGRectMake(5, 0, self.frame.size.width, self.frame.size.height / 66 * 27
    self.flayout.itemSize = CGSizeMake(335, 260);
    self.flayout.minimumLineSpacing = 10;
    self.flayout.minimumInteritemSpacing = 10;
    self.flayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 10);
    self.flayout.headerReferenceSize = CGSizeMake(10, 100);
    self.flayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.4];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self createData];
    
//    [NSTimer  scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];
    
    
}

//- (void)click{
//    
//    self.i = self.i + 1;
//    self.flayout.sectionInset = UIEdgeInsetsMake( - 310 * self.i, 10, 10, 10);
//    if (self.i == 7) {
//        self.i = 0;
//    }
//    
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //       与 tableView 的重用不同, collectionView 需要注册
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSLog(@"%@", self.array);
    cell.backgroundColor = [UIColor whiteColor];
    cell.label.text = self.array[indexPath.row][@"title"];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:self.array[indexPath.row][@"image"]]];
    cell.labelProvice.text = [NSString stringWithFormat:@"¥%@", self.array[indexPath.row][@"price"]];
    cell.labelProviceSec.text = [NSString stringWithFormat:@"¥%@",self.array[indexPath.row][@"market_price"]];
    cell.labelCount.text = [NSString stringWithFormat:@"仅限%@份", self.array[indexPath.row][@"store"]];
    return cell;
    
}


- (void)createData{
    
    NSString *body = @"c_md5=d89cf0bdeba3c1a3525e9ba464e3e231&lon=4.9E-324&format=json&source=android&lat=4.9E-324&";
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    
    [WHZAfnetWorking POSTJRAfnNetworkingWithUrl:@"http://api.meishi.cc/v5/buy_index.php?format=json" WithParameters:dic Block:^(id result) {
        
        self.dictionary = result;
        self.array = self.dictionary[@"ad_top"];
        [self.collectionView reloadData];
    }];
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ShopJumpViewController *shopVC = [[ShopJumpViewController alloc] init];
    [self.VC.navigationController pushViewController:shopVC animated:YES];
    
}







@end
